//
//  LAB_GestureImageView.m
//  Camera
//
//  Created by Bodi on 16/8/17.
//  Copyright © 2016年 葛海一. All rights reserved.
//

#import "LAB_GestureImageView.h"

@implementation LAB_GestureImageView

- (instancetype)initWithImage:(UIImage *)image withFrame:(CGRect)frame{
    self = [super initWithImage:image];
    if (self) {
        self.userInteractionEnabled = YES;
        self.frame = frame;
        [self addTap];
        [self addSwip];
        [self addLong];
        [self addscr];
        [self addPinch];
        [self addpan];
        [self addRotation];

    }
    return self;

}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.frame = frame;
        [self addTap];
        [self addSwip];
        [self addLong];
        [self addscr];
        [self addPinch];
        [self addpan];
        [self addRotation];
        
    }
    return self;
}

/** 点击手势. */
- (void)addTap {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    /** 设置几个手指头点击, 默认一个. */
    tap.numberOfTouchesRequired = 2;
    
    /** 设置点击几下 */
    tap.numberOfTapsRequired = 2;
    
    
    
    [self addGestureRecognizer:tap];
    
    
    
}
- (void)tapAction:(UIGestureRecognizer *)tap{
    
//    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
    
}
/**清扫手势*/
- (void)addSwip{
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
    /** 设置轻扫方向*/
    swip.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swip];
    
    
    
}
- (void)swipAction:(UISwipeGestureRecognizer *)swip{
    
//    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
}

/**长按手势*/
- (void)addLong{
    UILongPressGestureRecognizer *lon =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
    [self addGestureRecognizer:lon];
    
    
    
}

- (void)longAction:(UILongPressGestureRecognizer *)lon{
//    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"sss" object:nil userInfo:nil];
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(-12,-12, 15, 15);
//    button.backgroundColor = [UIColor redColor];
//    [button addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:button];
    
    
}

//- (void)clear:(UIButton *)button{
//    NSLog(@"bbbbbbbś");
//    [self removeFromSuperview];
//    NSLog(@"ààaaaa");
//    [button removeFromSuperview];
//
//
//}

/**边缘轻扫*/
- (void)addscr{
    
    UIScreenEdgePanGestureRecognizer * scr = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(scrAction:)];
    [self addGestureRecognizer:scr];
   
    
}
- (void)scrAction:(UIScreenEdgePanGestureRecognizer *)scr{
    
//    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
}

/** 捏合手势 */
- (void)addPinch{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [self addGestureRecognizer:pinch];
    
    
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    
//    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
    /** transform 属性: 可以改变视图呈现的方式, 例如旋转, 缩放, 拖拽*/
    self.transform = CGAffineTransformScale(self.transform, pinch.scale, pinch.scale);
    pinch.scale = 1.0;
}


/**拖拽手势*/
- (void)addpan{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self addGestureRecognizer:pan];
//    NSLog(@"dddd");
    
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
//    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
    CGPoint point = [pan translationInView:self];
    self.transform = CGAffineTransformTranslate(self.transform, point.x, point.y);
    [pan setTranslation:CGPointZero inView:self];
    
//    NSLog(@"%f %f", self.frame.origin.x, self.frame.origin.y);
    NSString *str1 = [NSString stringWithFormat:@"%f",self.frame.origin.x];
    NSString *str2 = [NSString stringWithFormat:@"%f",self.frame.origin.y];
    
    NSArray *array = @[str1,str2];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"location" object:array userInfo:nil];
}
- (void)addRotation{
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [self addGestureRecognizer:rotation];
    ;
    
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
//    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    self.transform = CGAffineTransformRotate(self.transform, rotation.rotation);
    rotation.rotation = 0.0;
    
    
    
}

@end
